package zyf;

/**
 * 互斥锁
 * 多个同步锁片段，代码会互斥
 */
public class sy4 {
    public static void main(String[] args) {
        boo boo = new boo();
        Thread t1 = new Thread(){
            public void run(){
                boo.metha();
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                boo.methb();
            }
        };
        t1.start();
        t2.start();
    }
}
class  boo{
    public synchronized void metha(){
        Thread t =  Thread.currentThread();
        System.out.println(t.getName()+":正在执行A方法");
        try {
            Thread.sleep(2000);
            System.out.println(t.getName()+":执行A方法完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void methb(){
        Thread t =  Thread.currentThread();
        System.out.println(t.getName()+":正在执行B方法");
        try {
            Thread.sleep(2000);
            System.out.println(t.getName()+":执行B方法完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

